package com.boss.controller.Studeent;


import com.boss.pojo.Course;
import com.boss.pojo.Evaluations;
import com.boss.pojo.PageBean;
import com.boss.pojo.Result;
import com.boss.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;
    //根据用户id查询已选课程
    @GetMapping("course/{id}")
    public Result getById(@PathVariable Integer id,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询已选的课程参数为：userId:{},pageNum:{},pageSize:{}",id,pageNum,pageSize);
       /* List<Course> courseList = studentService.getCourseList(id);*/
        PageBean page = studentService.getPageByUserId(id, pageNum, pageSize);
        return Result.success(page);
    }

    //根据student_id和course_id查询评教
    @GetMapping("/evaluation/{studentId}/{courseId}")
    public Result getEvaluationById(@PathVariable Integer studentId, @PathVariable Integer courseId) {
        log.info("根据评教id查询评教,studentId:{},courseId:{}",studentId,courseId);
         Evaluations evaluation = studentService.getEvaluation(studentId, courseId);
        return Result.success(evaluation);
    }

    //历史评教查询
    @GetMapping("/history/{id}")
    public Result getHistoryById(@PathVariable Integer id,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询历史的评教参数为:{},{},{}",id,pageNum,pageSize);
        /*List<Evaluations> courseList = studentService.getEvaluationList(id);*/
        PageBean courseList = studentService.getEvaluationList(id, pageNum, pageSize);
        return Result.success(courseList);
    }

    //添加课程评价
    @PostMapping("/evaluation")
    public Result addEvaluation(@RequestBody Evaluations evaluations) {
        log.info("添加课程评价,evaluations:{}",evaluations);
        Integer studentId = evaluations.getStudentId();
        Integer courseId = evaluations.getCourseId();
        if (studentService.getCourseList(studentId).stream().noneMatch(course -> course.getCourseId().equals(courseId))) {
            return Result.error("该课程未选修");
        }
        if (studentService.getEvaluation(studentId, courseId) != null) {
            return Result.error("已评价过该课程，无需再评教");
        }
        studentService.addEvaluation(evaluations);
        return Result.success();
    }














}
